<template>
    <div>
        <el-dialog v-model="props.visible" title="废料列表" width="70%" @close="handleCancel">
            <el-form :model="queryParams" ref="queryRef" :inline="true">
                <el-row>
                    <el-form-item label="处理单编码">
                        <el-input v-model="queryParams.code" placeholder="请输入处理单编码" clearable style="width: 200px"
                            @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item label="处理单位">
                        <el-input v-model="queryParams.handleDept" placeholder="请输入处理单位" clearable style="width: 200px"
                            @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-row>

            </el-form>
            <el-button style="margin-bottom: 5px;" type="primary" plain @click="handleSubmit"
                       :disabled="multiple">确认</el-button>
            <el-table ref="dataListRef" v-loading="loading" :data="wasteList" stripe row-key="id"  border style="cursor: pointer;"
                      @selection-change="handleSelectionChange"> <!--@cell-click="handleCellClick"-->
                <el-table-column type="selection" :reserve-selection="true" align="center" width="65" />
                <el-table-column label="序号" width="80" type="index" align="center" />
                <el-table-column label="状态" align="center" prop="status">
                    <template #default="scope">
                        <dict-tag :options="mes_handle_state" :value="scope.row.status" />
                    </template>
                </el-table-column>
                <el-table-column label="处理单编码" align="center" prop="code">
                </el-table-column>
                <el-table-column label="制单时间" align="center" prop="handleDate" />
                <el-table-column label="总金额" align="center" prop="amountSum" />
                <el-table-column label="已收金额" align="center" prop="amountReceived">
                    <template #default="scope">
                        {{ scope.row.amountReceived === null ? 0 : scope.row.amountReceived }}
                    </template>
                </el-table-column>
                <el-table-column label="经办人" align="center" prop="operator" />
                <el-table-column label="处理单位" align="center" prop="handleDept" />

                <el-table-column label="备注" align="center" prop="remark" />
            </el-table>
            <div class="footer">
                <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize" @pagination="getList" />
            </div>
        </el-dialog>
    </div>
</template>

<script setup>
const emit = defineEmits(['changeUpdate'])

import { listWaste } from '@/api/scrap/handle.js'

const { proxy } = getCurrentInstance();
const { mes_handle_state, } = proxy.useDict('mes_handle_state',);
const selectRow = ref([])
const multiple = ref(true)
const dataListRef =ref(null)
const props = defineProps({
    visible: {
        type: Boolean,
        default: false
    },

})
watch(() => props.visible, (val) => {
    if (val) {
        handleQuery()
        nextTick(() => {
            dataListRef.value.clearSelection()
            if (props.receiptList!=undefined && props.receiptList.length > 0) {
                props.receiptList.forEach(item => {
                    dataListRef.value.toggleRowSelection(item, true)
                })
            }
        })
    }
})
const wasteList = ref([])
const loading = ref(false)
const queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    status: 0,
    code: undefined,
    handleDept: undefined

})
const total = ref(0)
// 选中当前行
const handleCellClick = (row) => {
    proxy.$modal.confirm('是否确认选择处理单编码「' + row.code + '」').then(function () {
        emit('changeUpdate', row)
    }).catch(() => { });
}

const handleCancel = () => {
    proxy.resetForm("queryRef");
    emit('cancel')
}
const getList = async () => {
    loading.value = true
    const res = await listWaste(queryParams.value)
    loading.value = false
    wasteList.value = res.rows
    total.value = res.total
}

// 搜索
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}
//提交
const handleSubmit = () => {
    emit('changeUpdate', selectRow.value)
}
// 选中
const handleSelectionChange = (row) => {
    console.log(row, 'row');
    selectRow.value = row
    multiple.value = !row.length
}
/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm("queryRef");
    handleQuery();
};
// getList()
</script>

<style lang="scss" scoped></style>